package acheng1314.cn.a3dbuild2.view.activity.presenter;

import com.acheng.achengutils.gsonutil.GsonUtils;
import com.acheng.achengutils.mvp.presenter.BasePresenter;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;
import com.kymjs.rxvolley.http.VolleyError;

import acheng1314.cn.a3dbuild2.MyApplication;
import acheng1314.cn.a3dbuild2.bean.ProjectItemBean;
import acheng1314.cn.a3dbuild2.hostApi.MyApi;
import acheng1314.cn.a3dbuild2.view.activity.viewcontroller.HomeActivityViewController;

/**
 * Created by Administrator on 2016/10/6 0006.
 */
public class HomeActivityPresenter extends BasePresenter<HomeActivityViewController> {
    /**
     * 在子类的构造函数中，设定参数为model，这时候可以presenter调用接口来实现对界面的操作。
     *
     * @param model
     */
    public HomeActivityPresenter(HomeActivityViewController model) {
        super(model);
    }

    @Override
    public void initData() {

    }

    public void loadListData(String uid, String token) {
        HttpParams params = new HttpParams();
        params.put("uid", uid);
        params.put("token", token);
        RxVolley.post(MyApi.ProjectsApi, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                super.onSuccess(t);
                MyApplication.getInstance().outLog("输出", t);
                try {
                    if (null != t) {
                        ProjectItemBean bean = new GsonUtils().toBean(t, ProjectItemBean.class);

                        if (bean.getResult().size() > 0) {
                            model.updateList(bean.getResult());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    model.showDailog("输出出错啦。\n请联系管理员。");
                }
            }

            @Override
            public void onFailure(VolleyError error) {
                super.onFailure(error);
                model.showDailog("获取数据失败，请检查网络");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                model.disProgressD();
            }

            @Override
            public void onPreStart() {
                super.onPreStart();
                model.showProgressD();
            }
        });
    }
}
